package com.example.project.dao.impl;

import com.example.project.dao.IBussinesstripDao;
import com.example.project.entity.Attendance;
import com.example.project.entity.Bussinesstrip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public class BussinesstripDaoImpl implements IBussinesstripDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //实现出差信息添加方法
    @Override
    public int add(Bussinesstrip bussinesstrip) {
            return jdbcTemplate.update("insert into bussinesstrip(TripId,StartDate,EndDate,Description,Ratifiers,EmployeeId)"+ "values (?,?,?,?,?,?)",
                    bussinesstrip.getTripId(),
                    bussinesstrip.getStartDate(),
                    bussinesstrip.getEndDate(),
                    bussinesstrip.getDescription(),
                    bussinesstrip.getRatifiers(),
                    bussinesstrip.getEmployeeId()
               );
    }

    //实现查询所有出差数据的方法
    @Override
    public List<Bussinesstrip> getAllBussinesstrip(Bussinesstrip bussinesstrip) {
        return jdbcTemplate.query("select TripId,StartDate,EndDate,Description,Ratifiers,EmployeeId from bussinesstrip",new Object[]{},
                new BeanPropertyRowMapper<>(Bussinesstrip.class));
    }

    //删除打卡数据
    @Override
    public int deleteBussinesstrip(Bussinesstrip bussinesstrip) {
        return jdbcTemplate.update("delete from bussinesstrip where TripId=?",
               bussinesstrip.getTripId());
    }

    //实现出差信息的更新
    @Override
    public int updateBussinesstrip(Bussinesstrip bussinesstrip) {
        return jdbcTemplate.update("update bussinesstrip set"+" StartDate=?,EndDate=?,Description=?,Ratifiers=?,EmployeeId=? where TripId=?",
                bussinesstrip.getStartDate(),
                bussinesstrip.getEndDate(),
                bussinesstrip.getDescription(),
                bussinesstrip.getRatifiers(),
                bussinesstrip.getEmployeeId(),
                bussinesstrip.getTripId());
    }

    //实现单挑查询出差数据方法
    @Override
    public Bussinesstrip getBussinesstrip(Bussinesstrip bussinesstrip) {
        List<Bussinesstrip> result =jdbcTemplate.query("select"+"`TripId`,`StartDate`,`EndDate`,`Description`,`Ratifiers`,`EmployeeId`"+"from bussinesstrip where TripId='"+bussinesstrip.getTripId()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Bussinesstrip.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }

}
